package org.funtester.performance.books.chapter09.section3;

import com.alibaba.fastjson.JSON;

/**
 * 用户行为埋点数据
 */
public class UserBehavior {

    public int id;// 用户ID

    public String action;// 用户行为

    public String content;// 用户行为内容

    public long timestamp;// 用户行为时间戳

    public UserBehavior() {
    }

    /**
     * 只初始化用户行为的构造方法
     * @param action 用户行为
     */
    public UserBehavior(String action) {
        this.action = action;
    }

    /**
     * @param id        用户ID
     * @param action    用户行为
     * @param content   用户行为内容
     * @param timestamp 用户行为时间戳
     */
    public UserBehavior(int id, String action, String content, long timestamp) {
        this.id = id;
        this.action = action;
        this.content = content;
        this.timestamp = timestamp;
    }

    /**
     * 转换成json字符串
     * @return json字符串
     */
    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }

    /**
     * 清空数据
     */
    public void clear() {
        this.id = 0;
        this.action = null;
        this.content = null;
        this.timestamp = 0;
    }

    /**
     * 清空数据,保留action
     */
    public void clearKeepAction() {
        this.id = 0;
        this.content = null;
        this.timestamp = 0;
    }

    /**
     * 解析json字符串
     * @param json json字符串
     * @return
     */
    public static UserBehavior parse(String json) {
        return JSON.parseObject(json, UserBehavior.class);
    }

}
